<?php


namespace app\store\controller\wxapp;


use app\store\controller\Controller;
use app\store\model\WxappMenu as WxappMenuModel;

class Menu extends Controller
{
    /**
     * 列表
     */
    public function index()
    {
        $list = WxappMenuModel::getList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 新增
     */
    public function add(WxappMenuModel $wxappMenuModel)
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
        if ($wxappMenuModel->add($this->request->post('menu/a'))) {
            return $this->renderSuccess('添加成功', url('wxapp.menu/index'));
        }
        return $this->renderError($wxappMenuModel->getError() ?: '添加失败');
    }

    /**
     * 新增
     */
    public function edit($id)
    {
        $menu = WxappMenuModel::getMenuById($id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('menu'));
        }
        if ($menu->edit($this->request->post('menu/a'))) {
            return $this->renderSuccess('编辑成功', url('wxapp.menu/index'));
        }
        return $this->renderError($menu->getError() ?: '编辑失败');
    }

    public function delete($id)
    {
        $menu = WxappMenuModel::getMenuById($id);
        if ($menu->delete()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($menu->getError() ?: '删除失败');
    }
}